<template>
  <div class="login-container">
    <div class="login-box">
      <div class="login-left">
        <div class="title">
          <h2>智慧旅游管理系统</h2>
          <p>Travel Management System</p>
        </div>
        <div class="illustration">
          <img src="@/assets/images/login-bg.svg" alt="登录插图">
        </div>
      </div>
      <div class="login-form-container">
        <div class="login-form-title">
          <h3>系统登录</h3>
          <p>欢迎回来，请登录您的账号</p>
        </div>
        <el-form ref="loginForm" :model="loginForm" :rules="loginRules" class="login-form">
          <el-form-item prop="username">
            <el-input
              ref="username"
              v-model="loginForm.username"
              placeholder="请输入用户名"
              prefix-icon="el-icon-user"
              type="text"
              tabindex="1"
              auto-complete="off"
            />
          </el-form-item>

          <el-form-item prop="password">
            <el-input
              ref="password"
              v-model="loginForm.password"
              :type="passwordType"
              placeholder="请输入密码"
              prefix-icon="el-icon-lock"
              tabindex="2"
              auto-complete="off"
              @keyup.enter.native="handleLogin"
            >
              <i
                slot="suffix"
                class="el-icon-view password-icon"
                @click="showPwd"
              />
            </el-input>
          </el-form-item>

          <el-form-item prop="captcha">
            <div class="captcha-container">
              <el-input
                v-model="loginForm.captcha"
                placeholder="请输入验证码"
                prefix-icon="el-icon-key"
                tabindex="3"
              />
              <img
                :src="captchaUrl"
                class="captcha-img"
                @click="refreshCaptcha"
                alt="验证码"
              >
            </div>
          </el-form-item>

          <el-button
            :loading="loading"
            type="primary"
            class="login-button"
            @click.native.prevent="handleLogin"
          >
            登 录
          </el-button>
        </el-form>
      </div>
    </div>
  </div>
</template>

<script>
import { getCaptcha } from '@/api/user'
import { v4 as uuidv4 } from 'uuid'

export default {
  name: 'Login',
  data() {
    return {
      loginForm: {
        username: '',
        password: '',
        captcha: '',
        captchaKey: ''
      },
      loginRules: {
        username: [{ required: true, trigger: 'blur', message: '请输入用户名' }],
        password: [{ required: true, trigger: 'blur', message: '请输入密码' }],
        captcha: [{ required: true, trigger: 'blur', message: '请输入验证码' }]
      },
      loading: false,
      passwordType: 'password',
      captchaUrl: '',
      redirect: undefined
    }
  },
  watch: {
    $route: {
      handler: function(route) {
        this.redirect = route.query && route.query.redirect
      },
      immediate: true
    }
  },
  created() {
    this.refreshCaptcha()
  },
  methods: {
    showPwd() {
      this.passwordType = this.passwordType === 'password' ? '' : 'password'
      this.$nextTick(() => {
        this.$refs.password.focus()
      })
    },
    refreshCaptcha() {
      const key = uuidv4()
      this.loginForm.captchaKey = key
      this.captchaUrl = getCaptcha(key)
    },
    handleLogin() {
      this.$refs.loginForm.validate(valid => {
        if (valid) {
          this.loading = true
          this.$store.dispatch('user/login', this.loginForm)
            .then(async () => {
              this.$message({
                message: '登录成功',
                type: 'success',
                duration: 2000
              })
              
              // 等待一个短暂的时间，确保路由已经添加完成
              await this.$nextTick()
              
              // 使用 replace 而不是 push，避免在历史记录中留下重复的记录
              this.$router.replace(this.redirect || '/')
              
              this.loading = false
            })
            .catch(() => {
              this.refreshCaptcha()
              this.loading = false
            })
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.login-container {
  height: 100vh;
  width: 100vw;
  background: linear-gradient(135deg, #1890ff 0%, #1d39c4 100%);
  display: flex;
  align-items: center;
  justify-content: center;

  .login-box {
    width: 1000px;
    height: 600px;
    background: white;
    border-radius: 12px;
    box-shadow: 0 20px 40px rgba(0,0,0,0.1);
    display: flex;
    overflow: hidden;

    .login-left {
      flex: 1;
      background: #f5f7fa;
      padding: 40px;
      display: flex;
      flex-direction: column;

      .title {
        text-align: center;
        margin-bottom: 40px;

        h2 {
          color: #1890ff;
          font-size: 28px;
          margin-bottom: 10px;
        }

        p {
          color: #666;
          font-size: 16px;
        }
      }

      .illustration {
        flex: 1;
        display: flex;
        align-items: center;
        justify-content: center;
        
        img {
          width: 80%;
          height: auto;
        }
      }
    }

    .login-form-container {
      width: 400px;
      padding: 40px;
      display: flex;
      flex-direction: column;

      .login-form-title {
        text-align: center;
        margin-bottom: 40px;

        h3 {
          font-size: 24px;
          color: #333;
          margin-bottom: 10px;
        }

        p {
          color: #999;
          font-size: 14px;
        }
      }
    }
  }
}

.login-form {
  .el-form-item {
    margin-bottom: 20px;
  }

  .el-input {
    height: 48px;

    ::v-deep .el-input__inner {
      height: 48px;
      line-height: 48px;
      border-radius: 24px;
      padding-left: 45px;
      border: 1px solid #e8e8e8;
      
      &:focus {
        border-color: #1890ff;
        box-shadow: 0 0 0 2px rgba(24,144,255,0.2);
      }
    }

    ::v-deep .el-input__prefix {
      left: 16px;
      height: 48px;
      line-height: 48px;
      color: #999;
    }
  }

  .password-icon {
    cursor: pointer;
    color: #999;
    
    &:hover {
      color: #1890ff;
    }
  }

  .captcha-container {
    display: flex;
    align-items: center;
    gap: 12px;

    .el-input {
      flex: 1;
    }

    .captcha-img {
      height: 48px;
      border-radius: 4px;
      cursor: pointer;
    }
  }

  .login-button {
    width: 100%;
    height: 48px;
    border-radius: 24px;
    font-size: 16px;
    margin-top: 20px;
  }
}
</style> 